package com.main.ok.schedule;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.springframework.scheduling.support.TaskUtils;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

public class Executor implements Runnable {
    public static void main(String[] args) {
        new Executor().buildSchedule();
    }

    public void buildSchedule() {
        Runnable task = TaskUtils.decorateTaskWithErrorHandler(this, null, true);
        // 创建单线程的SechdulerExecutor,并用guava的ThreadFactoryBuilder设定生成线程的名称
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder()
                .setNameFormat("JdkTimerJob-%1$d").build());
        
        scheduledExecutorService.scheduleAtFixedRate(task, 0, 5, TimeUnit.SECONDS);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName() + ">>hello,time");
    }
}
